/**
 * Created by Administrator on 2017/4/20.
 */

var Room = require('../../game/room').Room;


var card9PublicCards = require('./publicCards').PublicCards;
var SubCode = require('../subCode');

exports.Card9Room = Room.extend({
    _publicCards: null,        // 公牌

    _dealerIndex: 0,           // 庄家坐位
    _dealerLZ: 0,              // 连庄次数

    _curPlay: 0,               // 当前玩家座位号

    init: function (rid, creator, msg) {
        this._super(rid, creator, msg);

        this._publicCards = new card9PublicCards(this);

        this._dealerIndex = 0;
        this._dealerLZ = 0;
        this._curPlay = 0;
    },

    getPublicCards: function () {
        return this._publicCards;
    },

    getDealerPlayer: function () {
        return this.getPlayerByIndex(this._dealerIndex);
    },

    getDealerIndex: function () {
        return this._dealerIndex;
    },

    getDealerLZ: function () {
        return this._dealerLZ;
    },

    getCurPlay: function () {
        return this._curPlay;
    },

    // 设置当前玩家座位号
    setCurPlay: function (curPlay) {
        this._curPlay = curPlay;
    },

    getCurPlayer: function () {
        return this.getPlayerByIndex(this._curPlay);
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getInfo: function () {
        var info = this._super();
        info.dealer = this._dealerIndex;
        info.dealerLZ = this._dealerLZ;
        info.curPlay = this._curPlay;
        return info;
    },

    getReconnectInfo: function (rePlayer) {
        var info = this._super(rePlayer);
        info.dealer = this._dealerIndex;
        info.dealerLZ = this._dealerLZ;
        info.curPlay = this._curPlay;
        info.publicCardNum = this._publicCards.getRemainNum();
        
        info.playedCards = this._publicCards.getPlayedCards();
        return info;
    },

    getSettlementInfo: function () {
        var info = this._super();
        info.dealerIndex = this.getDealerIndex();
        info.dealerLZ = this.getDealerLZ();
        return info;
    },

    onBeforeStartNewRound: function () {
        this._super();
        if(this._publicCards.isEmpty()){
            this._publicCards.gen();
        }
        this._sendPlayedCards();
        //this._publicCards.gen();

        // 开始回合清空当前玩家
        this._curPlay = 0;
    },

    /******************************************************************
     * 功能接口
     ******************************************************************/
    /**
     * 给玩家发牌
     */
    initPlayersHandCards: function () {
        var remainNum = 4;
        var needNum = 0;
        this.enumPlaying(function (eIndex, ePlayer) {
            if (!needNum) {
                needNum = ePlayer.getInitHandCardNeedNum();
            }
            i -= 1;
            var ready = this._publicCards.getCards(ePlayer.getInitHandCardNeedNum(), ePlayer.getInitHandCardPreCards());
            ePlayer.initHandCards(ready);
        }.bind(this));


        for (var i = 0; i < remainNum; ++i) {
            this._publicCards.getCards(needNum);
        }

        this._sendPublicCardChanged();
    },

    /**
     * 发送公牌数量
     * @private
     */
    _sendPublicCardChanged: function () {
        this.broadcast("onPublicCardChanged", {
            "cardNum": this._publicCards.getRemainNum(),
        });
    },

    _sendPlayedCards:function(){
        var temp = this._publicCards.getPlayedCards();
        this.broadcast("onPlayedCards",{
            "cards":temp
        })
    },


    //gm接口
    handleGmSetPublicCards: function (player, msg, callback) {
        if (!player.isSuperGM()) {
            callback({ code: Code.GM_LEVEL_LIMIT }); return;
        }

        this._publicCards.setRemainCards(msg.cards);
        callback({});
    },
    handleGmSetHandCards: function (player, msg, callback) {
        player.setInitHandCardPreCards(msg.cards);
        callback({});

    },
    handleGmGetPublicCards: function (player, msg, callback) {
        if (!player.isSuperGM()) {
            callback({ code: Code.GM_LEVEL_LIMIT }); return;
        }

        callback({ "cards": this._publicCards.getRemainCards() });
    },
});